import { Injectable } from '@angular/core';
import { AlertController } from 'ionic-angular';

/**
 * @Auther: ayo
 * @Date: 2018/8/23 17:01
 * @Description: confirm工具类
 */
@Injectable()
export class ConfirmProvider {
    constructor(private alertCtrl: AlertController) {}

    /**
     * confirm确认框
     */
    confirm(content, ok_callback = () => {
    }) {
        // enableBackdropDismiss 是否点击背景关闭弹窗
        const alert = this.alertCtrl.create({
            title: '<div class="content_img">提示</div>',
            subTitle: '',
            message: content,
            cssClass: '',
            enableBackdropDismiss: false,
            buttons: [{
                text: '取消', role: 'cancel',
            }, {
                text: '确定', cssClass: 'pop_btn', handler: () => {
                    if (ok_callback !== undefined && ok_callback !== null && typeof ok_callback === 'function') {
                        ok_callback();
                    }
                }
            }]
        });
        alert.present();
    }

    /**
     * 自定义confirm确认框
     */
    confirmDIY(obj, esc_callback: any = () => {
    }, ok_callback: any = () => {
    }) {
        const confirm_diy = this.alertCtrl.create({
            title: obj.title || '<div class="content_img"><img  src="assets/img/use_over.png" class="img"/></div>',
            subTitle: obj.subTitle || '',
            cssClass: obj.cssClass || '',
            message: '<div>' + obj.content + '</div>',
            enableBackdropDismiss: false,
            buttons: [{
                text: obj.escText || '取消', handler: () => {
                    if (esc_callback !== undefined && esc_callback !== null && typeof esc_callback === 'function') {
                        esc_callback();
                    }
                }
            }, {
                text: obj.okText || '确定', cssClass: 'pop_btn', handler: () => {
                    if (ok_callback !== undefined && ok_callback !== null && typeof ok_callback === 'function') {
                        ok_callback();
                    }
                }
            }]
        });
        confirm_diy.present();
    }
}
